/*
 *  POJ2676: Sudoku
 */

/*-------- Includes --------*/
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

/*-------- Types --------*/
struct Square {
    int pos;    // position of square
    int cnt;    // number of possible digits
};

/*-------- Global Variables --------*/
char sudo[9][10];  // sudoku storage
bool rowm[9][9];    // row mark
bool colm[9][9];    // column mark
bool boxm[9][9];    // box mark

Square sqr[81];
int    nsqr;

/*-------- Function Prototypes --------*/
bool compar(Square a, Square b);
bool dfs(int ptr);

/*-------- Main Function --------*/
int main(int argc, char const *argv[])
{
    int nCase;

    cin >> nCase;

    while (nCase--) {
        for (int i = 0; i < 9; i++)
            cin >> sudo[i];

        // initialize rowm[], colm[], boxm[]
        memset(rowm, 0, sizeof(rowm));
        memset(colm, 0, sizeof(colm));
        memset(boxm, 0, sizeof(boxm));

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                int n = sudo[i][j] - '0';
                if (n) {
                    int k = i / 3 * 3 + j / 3;  // better solution?

                    rowm[i][n-1] = true;
                    colm[j][n-1] = true;
                    boxm[k][n-1] = true;
                }
            }
        }

        // initialize sqr[]
        nsqr = 0;
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (sudo[i][j] != '0') continue;

                int k = i / 3 * 3 + j / 3;
                int c = 9;

                for (int l = 0; l < 9; l++)
                    if (rowm[i][l] || colm[j][l] || boxm[k][l])
                        c--;

                sqr[nsqr  ].pos = i * 9 + j;
                sqr[nsqr++].cnt = c;
            }
        }

        // sort sqr[]
        sort(sqr, sqr + nsqr, compar);

        // depth first search
        if (dfs(0))
            for (int i = 0; i < 9; i++)
                cout << sudo[i] << endl;
    }

    return 0;
}

/*-------- Functions --------*/
bool compar(Square a, Square b)
{
    return a.cnt < b.cnt;
}

bool dfs(int ptr)
{
    // base case: reach the bottom
    if (ptr == nsqr) return true;

    int pos = sqr[ptr].pos;

    int r = pos / 9;
    int c = pos % 9;
    int b = r / 3 * 3 + c / 3;

    for (int i = 0; i < 9; i++) {
        // cut off
        if (rowm[r][i] || colm[c][i] || boxm[b][i])
            continue;

        rowm[r][i] = true;
        colm[c][i] = true;
        boxm[b][i] = true;

        sudo[r][c] = i + '1';

        if (dfs(ptr + 1)) return true;

        // revert for backtracing
        rowm[r][i] = false;
        colm[c][i] = false;
        boxm[b][i] = false;
    }

    return false;
}
